#ifndef _COMPAT_QUEUE_H_
#define _COMPAT_QUEUE_H_

#include <compat/define.h>


#ifdef	__cplusplus
extern "C" 
{
#endif


typedef struct _queue_s  _queue_t;

struct _queue_s {
	_queue_t  *prev;
	_queue_t  *next;
};


#define _queue_init(q)															\
    (q)->prev = q;																\
    (q)->next = q

#define _queue_empty(h)															\
    (h == (h)->next)


#define _queue_insert_head(h, x)												\
    (x)->next = (h)->next;														\
    (x)->next->prev = x;														\
    (x)->prev = h;																\
    (h)->next = x


#define _queue_insert_after   _queue_insert_head


#define _queue_insert_tail(h, x)												\
    (x)->prev = (h)->prev;														\
    (x)->prev->next = x;														\
    (x)->next = h;																\
    (h)->prev = x


#define _queue_head(h)															\
    (h)->next


#define _queue_last(h)															\
    (h)->prev


#define _queue_sentinel(h)														\
    (h)


#define _queue_next(q)															\
    (q)->next


#define _queue_prev(q)															\
    (q)->prev


#define _queue_remove(x)														\
    (x)->next->prev = (x)->prev;												\
    (x)->prev->next = (x)->next


#define _queue_split(h, q, n)													\
    (n)->prev = (h)->prev;														\
    (n)->prev->next = n;														\
    (n)->next = q;																\
    (h)->prev = (q)->prev;														\
    (h)->prev->next = h;														\
    (q)->prev = n;


#define _queue_add(h, n)														\
    (h)->prev->next = (n)->next;												\
    (n)->next->prev = (h)->prev;												\
    (h)->prev = (n)->prev;														\
    (h)->prev->next = h;


#define _queue_data(q, type, link)                                         \
    (type *) ((u_char *) q - offsetof(type, link))




typedef _queue_t*	(*func__queue_middle)(_queue_t *queue);
typedef void		(*func__queue_sort)(_queue_t *queue, int(*cmp)(const _queue_t *, const _queue_t *));

STD_METHOD(_queue_t*) queue_middle(_queue_t *queue);
STD_METHOD(void) queue_sort(_queue_t *queue, int(*cmp)(const _queue_t *, const _queue_t *));


#ifdef	__cplusplus
}
#endif

#endif